/**
 * 
 */
package br.ufrgs.inf.cwsmart.description;

import br.ufrgs.inf.cwsmart.CWSMartFramework;
import br.ufrgs.inf.cwsmart.exception.FrameworkException;
import br.ufrgs.inf.cwsmart.util.Logger;

/**
 * Abstract class for a Description module which specify a CWS 
 * according to a set of arguments prior to having the obtained 
 * specification stored in the repository of CWS descriptions.  
 * 
 * @author Rodrigo S Lumertz
 *
 */
public abstract class Description {

	/**
	 * The constructor automatically called by the class that implements the matcher 
	 */
	public Description() {			
	}
	
	/**
	 * Adds a CWS to the repository of this mart 
	 * 
	 * @param cwsPath A CWS specification a la BPEL for example 
	 * @param language The language of the CWS specification
	 * 
	 * @throws FrameworkException Throws in case of some error during
	 * the parsing, the persisting operation or if the language os the CWS 
	 * specification is not supported
	 */
	public void addCWS(String cwsPath, String language, String... wsdlPaths) 
		throws FrameworkException {		
		
		Logger.logger.log(Logger.INFORMATIONAL_LEVEL, "Description.addCWS", 
				"CWS to be added: " + cwsPath);
		
		Logger.logger.log(Logger.DEBUG2_LEVEL, "Description.addCWS", "Instantiatiang CWSDescription");
		CWSDescription cws = instantiate(cwsPath, language, wsdlPaths);
		
		if (cws != null) {
			Logger.logger.log(Logger.DEBUG2_LEVEL, "Description.addCWS", "Parsing the cws description...");
			cws.parse();
			
			Logger.logger.log(Logger.DEBUG2_LEVEL, "Description.addCWS", "Persisting the cws data...");
			CWSMartFramework.getInstance().
				getRepositoryManagerInstance().persistCWS(cws);
		} else {
			throw new FrameworkException(FrameworkException.ErrorCode.EXTENSIBLE_INSTANCE_NOT_FOUND);
		}
	}
	
	/**
	 * Instantiate a class that knows how to proceed with the CWS description
	 * language in order to obtain properties of the CWS
	 * 
	 * @param cwsPath The CWS description file
	 * @param language The CWS description file language
	 * @param wsdlPaths The wsdl files
	 * 
	 * @return An instance of a class that can operate the CWS description
	 * @throws FrameworkException An exception in case of language not supported
	 */
	protected abstract CWSDescription instantiate(String cwsPath, String language, String... wsdlPaths) 
		throws FrameworkException;
}
